package com.invengo.library_rfid.analyze;

import com.invengo.library_rfid.analyze.impl.BenBuEPCStrategyImpl;
import com.invengo.library_rfid.analyze.impl.CollegesEPCStrategyImpl;
import com.invengo.library_rfid.analyze.impl.DUKEStrategyImpl;
import com.invengo.library_rfid.analyze.impl.DefaultEPCStrategyImpl;
import com.invengo.library_rfid.analyze.impl.FuNingEPCStrategyImpl;
import com.invengo.library_rfid.analyze.impl.InvengoEPCStrategyImpl;
import com.invengo.library_rfid.analyze.impl.ShanXiShiFanEPCStategyImpl;
import com.invengo.library_rfid.classify.RfidModelImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import java8.util.Optional;
import me.goldze.mvvmhabit.BuildConfig;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.cache.ACache;
import me.goldze.mvvmhabit.utils.cache.AKey;

/**
 * EPC解析工厂类
 */
public class EPCAnalyzeFactory {
    private final static EPCAnalyzeFactory FACTORY = new EPCAnalyzeFactory();
    private List<EPCStrategy> epcStrategyList = new ArrayList<>();

    /**
     * 工厂，每个匹配规则都需要在这里注册下，不注册使用默认规则
     */ {
        //超高频
        epcStrategyList.add(new DefaultEPCStrategyImpl());
        epcStrategyList.add(new InvengoEPCStrategyImpl());
        epcStrategyList.add(new BenBuEPCStrategyImpl());
        epcStrategyList.add(new CollegesEPCStrategyImpl());
        epcStrategyList.add(new DUKEStrategyImpl());
        epcStrategyList.add(new FuNingEPCStrategyImpl());
        epcStrategyList.add(new ShanXiShiFanEPCStategyImpl());
    }


    private EPCAnalyzeFactory() {
    }

    public static EPCAnalyzeFactory getInstance() {
        return FACTORY;
    }

    public List<EPCStrategy> getEpcStrategyList() {
        return epcStrategyList;
    }

    /**
     * 获取具体的模板类，
     *
     * @param epcRuleTag 规则标记
     * @return {@link EPCStrategy}
     */
    public EPCStrategy getEPCStrategy(_EpcRuleTag epcRuleTag) {
        EPCStrategy epcStrategy = epcStrategyList.get(0);
        for (EPCStrategy strategy : epcStrategyList) {
            if (strategy.getRule() == epcRuleTag) {
                epcStrategy = strategy;
            }
        }
        return epcStrategy;
    }

    public EPCStrategy getRuleStrategy() {
        EPCStrategy strategy = (EPCStrategy) ACache.getInstance().getAsObject(AKey.RFID_RULE_MODEL);
        if (strategy == null)
            return getEPCStrategy(_EpcRuleTag.RULE_DEFAULT);
        return strategy;
    }

    public int getPosition() {
        EPCStrategy ruleStrategy = getRuleStrategy();
        int i = getRuleList().indexOf(ruleStrategy.getRule());
        return i;
    }

    public List<_EpcRuleTag> getRuleList() {
        List<_EpcRuleTag> epcRuleTags = Arrays.asList(_EpcRuleTag.values());
        return epcRuleTags;
    }

    public void changeRuleModel(int which) {
        ACache.getInstance().put(AKey.RFID_RULE_MODEL, getEPCStrategy(getRuleList().get(which)));
    }
}
